/**
	Copyright (c) 2010 ATTi 
	All rights reserved.
	Author: Michael Shu
	Date: 1/22/2010
	Depreciated since s142 on 6/17/2011
*/

trigger AccManagerChangeOwnerInAccount on Account (after insert, after update) {
	/*
	String curAccManager, newAccManager; // The name of the users with the access and being granted access
	ID curAccManagerID, curAccId, AdvancedAdSolutions_RecordTypeId; // The ID of the user with the access
	AccountShare accShr; // Account Share object
	List<User> lUser = new List<User>(); // all account manager user list
	List<Account> lNewAcc = new List<Account>(); // all account records need to be shared
	List<AccountShare> lAccShrs = new List<AccountShare>();// Create a list of all AccountShare objects 
	List<AccountShare> lOldAccShrs = new List<AccountShare>();// Create a list of AccountShare objects to be deleted
	List<AccountShare> lNewAccShrs = new List<AccountShare>();// Create a list of AccountShare objects to be inserted.
	Set<ID> sCurAccManagerId = new Set<ID>(); // set of old account manager Ids
	Set<String> sNewAccManagerName = new Set<String>(); // set of new account manager names
	Map<String, ID> mAccManagerIdbyName = new Map<String, ID>(); // map account manager user Id by ccount manager user name
	Map<ID, String> mCurAccManagerName = new Map<ID, String>(); // all current account Manager Name by account Id
	if(Trigger.isAfter){
		if(AdvancedAdSolutions_RecordTypeId == null)
			AdvancedAdSolutions_RecordTypeId = RecordTypeHelper.GetRecordTypeIDByDevName('Account', 'Advanced_Ad_Solutions');
		
		if(Trigger.isUpdate){
			for(Account a_update : Trigger.new){
				if(a_update.RecordTypeId == AdvancedAdSolutions_RecordTypeId){
					curAccManager = Trigger.oldMap.get(a_update.Id).Account_Manager__c;
					newAccManager = a_update.Account_Manager__c;
					if(curAccManager != null && newAccManager != null && curAccManager != newAccManager){// if Account Mananger changed
						mCurAccManagerName.put(a_update.Id, curAccManager); // old Acccount Manager manual shared rules need to be deleted
						newAccManager = a_update.Account_Manager__c;
						sNewAccManagerName.add(newAccManager); // get all Account Manager Name list
						lNewAcc.add(a_update); // get all account records list
					}
				}
			}
		}
		if(Trigger.isInsert){
			for(Account a : Trigger.new){
				if(a.RecordTypeId == AdvancedAdSolutions_RecordTypeId){
					if(a.Account_Manager__c != null){	
						newAccManager = a.Account_Manager__c;	
						sNewAccManagerName.add(newAccManager); // get all Account Manager Name list
						lNewAcc.add(a); // get all account records list
					}
				}
			}
		}
		if(!lNewAcc.isEmpty()){ // if mCurAccManagerName is not empty, lNewAcc must not be empty as well, so just check lNewAcc
			lUser = [select u.Id, u.Name from User u 
				     where u.IsActive = true and (u.Name In: mCurAccManagerName.values() or u.Name In: sNewAccManagerName)
				     Order By u.Name]; // get the information of all users being granted access
			for(User u : lUser){
				mAccManagerIdbyName.put(u.Name, u.Id);
			}
			// delete old Account Share
			if(!mCurAccManagerName.isEmpty()){
				for(String oldname : mCurAccManagerName.values()){
					sCurAccManagerId.add(mAccManagerIdbyName.get(oldname)); // get old Account Manager Id set
				}
				lAccShrs = [select Id, AccountId, UserOrGroupId from AccountShare 
				              where AccountId In: mCurAccManagerName.keySet() and RowCause =: 'Manual' and UserOrGroupId In: sCurAccManagerId];//get all old accountshare Id
				for(AccountShare accShr_old : lAccShrs){
					curAccId = accShr_old.AccountId;
					curAccManager = mCurAccManagerName.get(curAccId);
					curAccManagerID = mAccManagerIdbyName.get(curAccManager);
					if(accShr_old.UserOrGroupId == curAccManagerID){
						lOldAccShrs.add(accShr_old);
					}	
				}
				delete lOldAccShrs; // delet old Account share for current account manager
			}			
			// insert/update new Account Share
			for(Account a_new : lNewAcc){
				accShr = new AccountShare(); // create new Account share object for account sharing
				accShr.AccountId = a_new.Id;  // accound Id being shared
				accShr.UserOrGroupId = mAccManagerIdbyName.get(a_new.Account_Manager__c); // user id being granted access
				accShr.AccountAccessLevel = 'Read'; //Read Access, including transfer 
				accShr.OpportunityAccessLevel = 'Read'; //Read Access 
				accShr.CaseAccessLevel  = 'Read'; //Read Access 
				//accShr.ContactAccessLevel  = 'Read'; //Read Access 
				lNewAccShrs.add(accShr); // add to Account share list
			}
			try {
				Database.SaveResult[] lsr = Database.insert(lNewAccShrs,false); // Insert the new sharing records and capture the save result.
				for(Database.SaveResult sr : lsr){// Process the save results for insert.
					if(!sr.isSuccess()){
						Database.Error err = sr.getErrors()[0]; // Get the first save result error.
						System.debug('err.getMessage() = ' + err.getMessage());
						System.debug('err.getStatusCode() = ' + err.getStatusCode());
						if(!(err.getStatusCode() == StatusCode.FIELD_INTEGRITY_EXCEPTION
						&& err.getMessage().contains('AccessLevel'))){ // Error is not related to trivial access level.
						}
					}
				}
			} catch(DmlException e) {
				System.debug('The Apex sharing recalculation threw the following exception: ' + e.getMessage());
			}
		}
	}	
	*/
}